package com.winit.openapi.ums.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ums.spi.user.UserBusinessConfService;
import com.winit.ums.spi.user.command.AreaWarehouseCommand;
import com.winit.ums.spi.user.command.GetDistrictsCommand;
import com.winit.ums.spi.user.command.QueryAreaCustomerCommand;
import com.winit.ums.spi.user.command.QueryCommand;
import com.winit.ums.spi.user.command.UpdateBusinessconfCommand;
import com.winit.ums.spi.user.command.UpdateByUserNameCommand;
import com.winit.ums.spi.user.command.VildaiteIntelligentRecommendCommand;
import com.winit.ums.spi.user.vo.AreaWarehouseVo;
import com.winit.ums.spi.user.vo.IntelligentWhDistrictVo;
import com.winit.ums.spi.user.vo.UserBusinessConfVo;

@Controller
@RequestMapping(value = "/ums/userConf")
public class UserBusinessConfController extends BaseController {
    
    @Resource
    private UserBusinessConfService userBusinessConfService;
    
    /**
     * 根据用户名和业务类别控制是否开启有效
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/update")
    public String update(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        UpdateByUserNameCommand command = JSON.parseObject(requestMsg.getData().toString(), UpdateByUserNameCommand.class);
        String userId = CommandContext.getContext().getUserId();
        Long userId_Num;
        try{
            userId_Num=Long.valueOf(userId);
           
        }catch(Exception e){
            userId_Num=-1l; 
        }
        command.setUserId(userId_Num);
        Map<String, Object> data = new HashMap<String, Object>();
        boolean flag = true;
        try {
            userBusinessConfService.updateByUserName(command);
            responseMsg.setData(data);
        } catch (Exception e) {
            flag = false;           
            data.put("flag",flag);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }
    
    /**
     * 根据用户名和业务类别查询
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/query")
    public String query(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryCommand command = JSON.parseObject(requestMsg.getData().toString(), QueryCommand.class);
        String userId = CommandContext.getContext().getUserId();
        Long userId_Num;
        try{
            userId_Num=Long.valueOf(userId);
           
        }catch(Exception e){
            userId_Num=-1l; 
        }
        command.setUserId(userId_Num);
        List<UserBusinessConfVo> businessConfVos = userBusinessConfService.queryByUserNameAndClassCode(command); 
        
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("businessConfVos", businessConfVos);         
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    
    @RequestMapping(value = "/queryIntelligentWHArea")
    public String queryIntelligentWHArea(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        GetDistrictsCommand command = JSON.parseObject(requestMsg.getData().toString(), GetDistrictsCommand.class);
        List<IntelligentWhDistrictVo> businessConfVos = userBusinessConfService.queryIntelligentWHArea(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("businessConfVos", businessConfVos);         
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    /**
     * 修改配置
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/updateUserBusinessConf")
    public String updateUserBusinessConf(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	UpdateBusinessconfCommand command = JSON.parseObject(requestMsg.getData().toString(), UpdateBusinessconfCommand.class);
    	userBusinessConfService.updateUserBusinessConf(command);
        return SUCCESS;
    }
    
    /**
     * 根据客户编码与 业务类别编码判断客户是否开通   true：开通  false：未开通
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/checkIntelligentWHArea")
    public String checkIntelligentWHArea(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	VildaiteIntelligentRecommendCommand command = JSON.parseObject(requestMsg.getData().toString(), VildaiteIntelligentRecommendCommand.class);
        boolean bool = userBusinessConfService.vildaiteIntelligentRecommend(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("isOpen", bool);         
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    /**
     * 关联客户区域
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/connectAreaCustomer")
    public String connectAreaCustomer(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        AreaWarehouseCommand command = JSON.parseObject(requestMsg.getData().toString(), AreaWarehouseCommand.class);
        String userId = CommandContext.getContext().getUserId();
        Long userId_Num;
        try{
            userId_Num=Long.valueOf(userId);
           
        }catch(Exception e){
            userId_Num=-1l; 
        }
        command.setUserCode(userId_Num);
        userBusinessConfService.connectAreaCustomer(command); 
        
        Map<String, Object> data = new HashMap<String, Object>();
        boolean flag = true;
        try {
            userBusinessConfService.connectAreaCustomer(command);
            responseMsg.setData(data);
        } catch (Exception e) {
            flag = false;           
            data.put("flag",flag);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }
    
    
    @RequestMapping(value = "/queryAreaWarehouse")
    public String queryAreaCustomerByCustomerCode(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryAreaCustomerCommand command = JSON.parseObject(requestMsg.getData().toString(), QueryAreaCustomerCommand.class);
        AreaWarehouseVo areaWarehouse = userBusinessConfService.queryAreaCustomerByCustomerCode(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("areaWarehouse", areaWarehouse);         
        responseMsg.setData(data);
        return SUCCESS;
    }
    
   

}
